/**
 * 
 */
package co.gov.medellin.sdep.sisdep.gestion.service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import co.gov.medellin.sdep.sisdep.gestion.exception.BodegaException;
import co.gov.medellin.sdep.sisdep.gestion.exception.BodegaRuntimeException;
import co.gov.medellin.sdep.sisdep.gestion.exception.descriptor.BodegaMessageDescriptor;
import co.gov.medellin.sdep.sisdep.common.message.MessageDescriptorConstant;
import co.gov.medellin.sdep.sisdep.common.service.BaseService;
import co.gov.medellin.sdep.sisdep.model.Bodega;
import co.gov.medellin.sdep.sisdep.model.dao.impl.IBodegaDAOT;

/**
 * @author Usuario
 *
 */
public class BodegaServiceImpl extends BaseService implements IBodegaService, Serializable {

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                 init()
	//----------------------------------------------------------------------

	/**
	 * default serial version
	 */
	private static final long serialVersionUID = 1L;
	/** */
	private IBodegaDAOT bodegaDAO;

	/**
	 * 
	 */
	public BodegaServiceImpl() {
	}

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                  end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                     init()
	//----------------------------------------------------------------------

	/**
	 * 
	 */
	public void insert(Bodega bodega) throws BodegaException {
		try{
			bodega.setUsuarioCrea(getPrincipalUser());
			bodega.setFechaCrea(new Date());
			getBodegaDAO().insert(bodega);
		}catch (Exception e) {
			Object [] infoError = {bodega.getCodigo()};
			if(e.getCause().toString().indexOf("ORA-00001") >= 0){
				throw new BodegaException(new BodegaMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new BodegaException(new BodegaMessageDescriptor(MessageDescriptorConstant.SAVE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void update(Bodega bodega) throws BodegaException {
		try{
			bodega.setUsuarioModifica(getPrincipalUser());
			bodega.setFechaModifica(new Date());
			getBodegaDAO().update(bodega);
		}catch (Exception e) {
			Object [] infoError = {
				bodega.getCodigo()
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new BodegaRuntimeException(new BodegaMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new BodegaRuntimeException(new BodegaMessageDescriptor(MessageDescriptorConstant.UPDATE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void delete(Bodega bodega) throws BodegaException {
		try {
			bodega.setUsuarioModifica(getPrincipalUser());
			bodega.setFechaModifica(new Date());
			getBodegaDAO().delete(bodega);
		}catch (Exception e) {
			Object [] infoError = {
				bodega.getCodigo()
			};
			throw new BodegaRuntimeException(new BodegaMessageDescriptor(MessageDescriptorConstant.DELETE), infoError, e);
		}
	}

	/**
	 * @return 
	 * 
	 */
	public final List load(int start,int max, Bodega bodega) throws BodegaException {
		try{
			//return getBodegaDAO().getAllBodegaPaginated(start, max, criteria);
			return getBodegaDAO().load(bodega);
		}catch (Exception e) {
			Object [] infoError = null;
			throw new BodegaRuntimeException(new BodegaMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}

	/**
	 * 
	 */
	public final Bodega get(long id) throws BodegaException {
		try{
			return getBodegaDAO().findByPrimaryKey(id);
		}catch (Exception e) {
			Object [] infoError = {new Long(id)};
			throw new BodegaRuntimeException(new BodegaMessageDescriptor(MessageDescriptorConstant.GET), infoError, e);
		}
	}

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                      end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- getters and setters                                         init()
	//----------------------------------------------------------------------

	/**
	 * @author Usuario
	 *
	 */
	public IBodegaDAOT getBodegaDAO() {
		return bodegaDAO;
	}

	/**
	 * @author Usuario
	 *
	 */
	public void setBodegaDAO(IBodegaDAOT bodegaDAO) {
		this.bodegaDAO = bodegaDAO;
	}

	//----------------------------------------------------------------------
	//--- getters and setters                                          end()
	//----------------------------------------------------------------------

}
